<?php


/**
 * 分页组建ClinkPager扩展
 * @description page-tab-tog为分页的样式class
 * @author <[<xm 杭州>]>
 * @time 2016-01-29
 * @example
 * <div class="page-tab-tog">
 *    <?php $this->widget('MyLinkPager',array(
 *         'header' => '',  //取消分页字符
 *         'firstPageLabel' => '首页',
 *         'lastPageLabel' => '尾页',
 *         'prevPageLabel' => '&lt;',
 *         'nextPageLabel' => '&gt;',
 *         'pages' => $pages,
 *         'maxButtonCount'=>5,
 *         'cssFile'=>false,  //不加载自带css文件
 *         'htmlOptions' => array('class' => 'met_pager'),
 *     ));?>
 * </div>
 */

class MyLinkPager extends CLinkPager
{
    const CSS_FIRST_PAGE='first';
    const CSS_LAST_PAGE='last';
    const CSS_PREVIOUS_PAGE='previous';
    const CSS_NEXT_PAGE='next';
    const CSS_INTERNAL_PAGE='page';
    const CSS_HIDDEN_PAGE='hidden';
    const CSS_SELECTED_PAGE='Ahover';
    const CSS_TOTAL_PAGE='total_page';
    const CSS_TOTAL_ROW='total_row';

    public $firstPageCssClass=self::CSS_FIRST_PAGE;
    public $lastPageCssClass=self::CSS_LAST_PAGE;
    public $previousPageCssClass=self::CSS_PREVIOUS_PAGE;
    public $nextPageCssClass=self::CSS_NEXT_PAGE;
    public $internalPageCssClass=self::CSS_INTERNAL_PAGE;
    public $hiddenPageCssClass=self::CSS_HIDDEN_PAGE;
    public $selectedPageCssClass=self::CSS_SELECTED_PAGE;
    public $maxButtonCount=10;
    public $nextPageLabel;
    public $prevPageLabel;
    public $firstPageLabel;
    public $lastPageLabel;
    public $header;
    public $footer='';
    public $cssFile = false; //禁用Yii Pager CSS
    public $htmlOptions=array();

    //设置为true的时候，显示共X页，$this->forceTotalPage值优先该值
    public $mCountPage = true;

    //是否强制显示共x页，设置为true时，$this->mCountPage和$this->getPageRange()无效
    public $forceTotalPage = false;

    public function init()
    {
        $this->header = "";
        if($this->nextPageLabel===null)
            $this->nextPageLabel = Yii::t('yii','>');
        if($this->prevPageLabel===null)
            $this->prevPageLabel = Yii::t('yii','<');
        if($this->firstPageLabel===null)
          $this->firstPageLabel = Yii::t('yii','<<');
        if($this->lastPageLabel===null)
          $this->lastPageLabel = Yii::t('yii','>>');
        if($this->header===null)
            $this->header = Yii::t('yii','Go to page: ');

        if(!isset($this->htmlOptions['id']))
            $this->htmlOptions['id']=$this->getId();
        if(!isset($this->htmlOptions['class']))
            $this->htmlOptions['class']='yiiPager';
    }

    public function run()
    {
        $this->registerClientScript();
        $buttons=$this->createPageButtons();

        list($beginPage,$endPage)=$this->getPageRange();
        // 页数统计
        if ($this->forceTotalPage)
        {
            $buttons[] = CHtml::tag('a', array('class'=>'total'),'共'.$this->getPageCount().'页');
            $buttons[] =CHtml::tag('a', array(),'共'.$this->getItemCount().'条');
        } else {
            if ($this->mCountPage && $endPage > 0)
            {
                $buttons[] = CHtml::tag('a', array('class'=>'total'),'共'.$this->getPageCount().'页');
                $buttons[] =CHtml::tag('a', array(),'共'.$this->getItemCount().'条');
            }
        }

        if(empty($buttons))
            return;
        echo $this->header;

        echo CHtml::tag('div',$this->htmlOptions,implode("\n",$buttons));
        echo $this->footer;
    }


    /**
     * Creates a page button.
     * You may override this method to customize the page buttons.
     * @param string $label the text label for the button
     * @param integer $page the page number
     * @param string $class the CSS class for the page button.
     * @param boolean $hidden whether this page button is visible
     * @param boolean $selected whether this page button is selected
     * @return string the generated button
     */
    protected function createPageButton($label,$page,$class,$hidden,$selected)
    {
        if($hidden || $selected)
            $class.=' '.($hidden ? $this->hiddenPageCssClass : $this->selectedPageCssClass);

        //return '<li class="'.$class.'">'.CHtml::link( $label,$this->createPageUrl($page),array('class'=>$class) ).'</li>';
        return ''.CHtml::link( $label,$this->createPageUrl($page),array('class'=>$class) ).'';
    }

    /**
     * @return array the begin and end pages that need to be displayed.
     */
    protected function getPageRange()
    {
        $currentPage=$this->getCurrentPage();
        $pageCount=$this->getPageCount();

        $beginPage=max(0, $currentPage-(int)($this->maxButtonCount/2));
        if(($endPage=$beginPage+$this->maxButtonCount-1)>=$pageCount)
        {
            $endPage=$pageCount-1;
            $beginPage=max(0,$endPage-$this->maxButtonCount+1);
        }
        return array($beginPage,$endPage);
    }

    /**
     * Registers the needed client scripts (mainly CSS file).
     */
    public function registerClientScript()
    {
        if($this->cssFile!==false)
            self::registerCssFile($this->cssFile);
    }

    /**
     * Registers the needed CSS file.
     * @param string $url the CSS URL. If null, a default CSS URL will be used.
     */
    public static function registerCssFile($url=null)
    {
        if($url===null)
            $url=CHtml::asset(Yii::getPathOfAlias('system.web.widgets.pagers.pager').'.css');
        Yii::app()->getClientScript()->registerCssFile($url);
    }

}